<?php
/**
 * TinyMVC - MysqlResult.class.php file
 *
 * @author Adrian Wieczorek <adi(at)tinymvc(dot)org>
 * @package TinyMVC
 * @subpackage Database
 * @version $Id: MysqlResult.class.php 105 2011-01-11 23:41:18Z adi3ek $
 *
 */

/**
 * MySQL query result returned by {@link MysqlDriver::query} method
 * @package TinyMVC
 * @subpackage Database
 *
 */
class MysqlResult  {
	private $result = null;
	private $rowsAffected = 0;
	private $currentRow = null;

	public function __construct(&$result, &$connection) {
		if($result) {
			$this->result = $result;
		}
		if($connection) {
			$this->rowsAffected = @mysql_affected_rows($connection);
		}
	}

	/**
	 * get next result row
	 * @return array field => value array
	 */
	public function getNextRow() {
		$this->currentRow = @mysql_fetch_array($this->result);
		return is_array($this->currentRow) ? $this->currentRow : false;
	}

	/**
	 * get value of given field name for current result row
	 * @return string field value
	 */
	public function getField($name) {
		return $this->currentRow[$name];
	}

	/**
	 * get number of fields in result
	 * @return int number of fields
	 */
	public function getFieldsNum() {
		return @mysql_num_fields($this->result);
	}

	/**
	 * get number of rows in result
	 * @return int number of rows
	 */
	public function getRowsNum() {
		return @mysql_num_rows($this->result);
	}

	/**
	 * get number of rows affected by last performed query
	 * @return int number of rows affected
	 */
	public function getRowsAffected() {
 	return $this->rowsAffected;
	}

	/**
	 * free result resource
	 */
	public function free() {
		@mysql_free_result($this->result);
		$this->result = null;
	}
}
